package com.rongke.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.rongke.mapper.GoodsMapper;
import com.rongke.model.Goods;
import com.rongke.model.Sku;
import com.rongke.service.GoodsService;
import com.rongke.service.SkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @version : Ver 1.0
 * @GoodsServiceImpl
 * @商品ServiceImpl
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private SkuService skuService;

    /**
     * 获取主页显示的上架新品
     *
     * @return
     */
    @Override
    public List<Goods> selectGoodsByCondition() {
        return goodsMapper.selectGoodsByCondition();
    }

    @Override
    public void task1() {
        System.out.print("定时任务1。。。。1");
    }

    @Override
    public void task2() {
        System.out.print("定时任务2。。。。2");
    }

    @Override
    public void uptToNullGroup(Map<String, Object> ids) {
        goodsMapper.uptToNullGroup(ids);
    }

    @Override
    public List<Goods> getBuyGoods(Long muserId) {
        return goodsMapper.getBuyGoods(muserId);
    }

    @Override
    public Goods fetchGoodsInventoryStatus(Goods goods) {
        if (null == goods.getId())
            return goods;
        EntityWrapper<Sku> ew = new EntityWrapper<>();
        ew.eq("goods_id", goods.getId());
        List<Sku> skus = skuService.selectList(ew);
        if (skus.size() == 1) {
            Sku sku = skus.get(0);
            if (sku.getIfUniformSpec() == 0) {
                if (sku.getInventory() == 0) {
                    goods.setGoodsStatus(4);//表示统一规格库存不足状态
                }
            }
        }
        return goods;
    }


}
